
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2023原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.text.NumberFormat;

import org.springframework.lang.Nullable;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;

/**
 * Property editor for any Number subclass such as Short, Integer, Long,
 * BigInteger, Float, Double, BigDecimal. Can use a given NumberFormat for
 * (locale-specific) parsing and rendering, or alternatively the default
 * {@code decode} / {@code valueOf} / {@code toString} methods.
 *
 * <p>This is not meant to be used as system PropertyEditor but rather
 * as locale-specific number editor within custom controller code,
 * parsing user-entered number strings into Number properties of beans
 * and rendering them in the UI form.
 *
 * <p>In web MVC code, this editor will typically be registered with
 * {@code binder.registerCustomEditor} calls.
 *
 * @author Juergen Hoeller
 * @since 06.06.2003
 * @see Number
 * @see java.text.NumberFormat
 * @see org.springframework.validation.DataBinder#registerCustomEditor
 */
/**
 *任何Number子类的属性编辑器，如Short、Integer、Long、，
 *BigInteger、Float、Double、BigDecimal。可以使用给定的NumberFormat
 *（特定于语言环境）解析和呈现，或者默认
 *｛@code decode｝/｛@codevalueOf｝/{@codetoString｝方法。
 *
 *＜p＞这不是要用作系统PropertyEditor，而是
 *作为自定义控制器代码内的区域特定数字编辑器，
 *将用户输入的数字字符串解析为bean的number财产
 *并以UI形式呈现它们。
 *
 *＜p＞在web MVC代码中，此编辑器通常会注册到
 *｛@code binder.registerCustomEditor｝调用。
 *
 *@作者Juergen Hoeller
 *@自2003年6月6日
 *@参见编号
 *@参见java.text.NumberFormat
 *@see org.springframework.validation.DataBinder#registerCustomEditor
 */
public class CustomNumberEditor extends PropertyEditorSupport {

	private final Class<? extends Number> numberClass;

	@Nullable
	private final NumberFormat numberFormat;

	private final boolean allowEmpty;


	/**
	 * Create a new CustomNumberEditor instance, using the default
	 * {@code valueOf} methods for parsing and {@code toString}
	 * methods for rendering.
	 * <p>The "allowEmpty" parameter states if an empty String should
	 * be allowed for parsing, i.e. get interpreted as {@code null} value.
	 * Else, an IllegalArgumentException gets thrown in that case.
	 * @param numberClass the Number subclass to generate
	 * @param allowEmpty if empty strings should be allowed
	 * @throws IllegalArgumentException if an invalid numberClass has been specified
	 * @see org.springframework.util.NumberUtils#parseNumber(String, Class)
	 * @see Integer#valueOf
	 * @see Integer#toString
	 */
	/**
	 *使用默认值创建新的CustomNumberEditor实例
	 *用于分析的｛@code valueOf｝方法和｛@codetoString｝
	 *用于渲染的方法。
	 *＜p＞“allowEmpty”参数指明空字符串是否应该
	 *被允许解析，即被解释为｛@code null｝值。
	 *否则，在这种情况下会引发IllegalArgumentException。
	 *@param numberClass要生成的Number子类
	 *@param allowEmpty如果应允许空字符串
	 *如果指定了无效的numberClass，@throws IllegalArgumentException
	 *@see org.springframework.util.NumberUtils#parseNumber（字符串，类）
	 *@参见整数#valueOf
	 *@参见整数#toString
	 */
	public CustomNumberEditor(Class<? extends Number> numberClass, boolean allowEmpty) throws IllegalArgumentException {
		this(numberClass, null, allowEmpty);
	}

	/**
	 * Create a new CustomNumberEditor instance, using the given NumberFormat
	 * for parsing and rendering.
	 * <p>The allowEmpty parameter states if an empty String should
	 * be allowed for parsing, i.e. get interpreted as {@code null} value.
	 * Else, an IllegalArgumentException gets thrown in that case.
	 * @param numberClass the Number subclass to generate
	 * @param numberFormat the NumberFormat to use for parsing and rendering
	 * @param allowEmpty if empty strings should be allowed
	 * @throws IllegalArgumentException if an invalid numberClass has been specified
	 * @see org.springframework.util.NumberUtils#parseNumber(String, Class, java.text.NumberFormat)
	 * @see java.text.NumberFormat#parse
	 * @see java.text.NumberFormat#format
	 */
	/**
	 *使用给定的NumberFormat创建新的CustomNumberEditor实例
	 *用于解析和呈现。
	 *＜p＞allowEmpty参数表示空字符串是否应该
	 *被允许解析，即被解释为｛@code null｝值。
	 *否则，在这种情况下会引发IllegalArgumentException。
	 *@param numberClass要生成的Number子类
	 *@param numberFormat用于分析和呈现的numberFormat
	 *@param allowEmpty如果应允许空字符串
	 *如果指定了无效的numberClass，@throws IllegalArgumentException
	 *@see org.springframework.util.NumberUtils#parseNumber（字符串，类，java.text.NumberFormat）
	 *@see java.text.NumberFormat#parse
	 *@参见java.text.NumberFormat#格式
	 */
	public CustomNumberEditor(Class<? extends Number> numberClass,
			@Nullable NumberFormat numberFormat, boolean allowEmpty) throws IllegalArgumentException {

		if (!Number.class.isAssignableFrom(numberClass)) {
			throw new IllegalArgumentException("Property class must be a subclass of Number");
		}
		this.numberClass = numberClass;
		this.numberFormat = numberFormat;
		this.allowEmpty = allowEmpty;
	}


	/**
	 * Parse the Number from the given text, using the specified NumberFormat.
	 */
	/**
	 *使用指定的NumberFormat分析给定文本中的数字。
	 */
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (this.allowEmpty && !StringUtils.hasText(text)) {
			// Treat empty String as null value.
			// 将空字符串视为空值。
			setValue(null);
		}
		else if (this.numberFormat != null) {
			// Use given NumberFormat for parsing text.
			// 使用给定的NumberFormat分析文本。
			setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
		}
		else {
			// Use default valueOf methods for parsing text.
			// 使用默认值Of方法分析文本。
			setValue(NumberUtils.parseNumber(text, this.numberClass));
		}
	}

	/**
	 * Coerce a Number value into the required target class, if necessary.
	 */
	/**
	 *如有必要，将Number值强制到所需的目标类中。
	 */
	@Override
	public void setValue(@Nullable Object value) {
		if (value instanceof Number num) {
			super.setValue(NumberUtils.convertNumberToTargetClass(num, this.numberClass));
		}
		else {
			super.setValue(value);
		}
	}

	/**
	 * Format the Number as String, using the specified NumberFormat.
	 */
	/**
	 *使用指定的NumberFormat将数字格式化为字符串。
	 */
	@Override
	public String getAsText() {
		Object value = getValue();
		if (value == null) {
			return "";
		}
		if (this.numberFormat != null) {
			// Use NumberFormat for rendering value.
			// 使用NumberFormat显示值。
			return this.numberFormat.format(value);
		}
		else {
			// Use toString method for rendering value.
			// 使用toString方法呈现值。
			return value.toString();
		}
	}

}
